package com.android.thelastnoet;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.PopupMenu;
import androidx.appcompat.widget.Toolbar;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.android.thelastnoet.R;
import com.android.thelastnoet.db.UserMapper;

public class SettingActivity extends AppCompatActivity {

    SharedPreferences.Editor editor;
    SharedPreferences data;
    Intent intent;
    UserMapper userMapper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);

        userMapper=UserMapper.getUserMapper(this);

        Toolbar toolbar = findViewById(R.id.toolbar_lock);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        toolbar.setNavigationOnClickListener(v -> finish());

        intent=new Intent(SettingActivity.this,LoginActivity.class);
        editor = getSharedPreferences("data", MODE_PRIVATE).edit();
        data = getSharedPreferences("data", MODE_PRIVATE);
    }

    public void onClick(View view) {
        switch (view.getId()){
            case R.id.outLogin:
                editor.putString("isLogin","");
                editor.commit();
                startActivity(intent);
                break;
            case R.id.closeAccount:
                editor.clear();
                editor.commit();
                userMapper.deleteUser(data.getInt("id",0));
                startActivity(intent);
                break;
            case R.id.textSize:
            case R.id.backGround:
                showPopupMenu(view);
                break;
        }
    }


    private void showPopupMenu(View view) {
        // View当前PopupMenu显示的相对View的位置
        PopupMenu popupMenu = new PopupMenu(this, view);
        // menu布局
        switch (view.getId()){
            case R.id.textSize:
                popupMenu.getMenuInflater().inflate(R.menu.text_size_menu, popupMenu.getMenu());
            case R.id.backGround:
                popupMenu.getMenuInflater().inflate(R.menu.group_name_menu, popupMenu.getMenu());

        }
        // menu的item点击事件
        popupMenu.setOnMenuItemClickListener(item -> {
            Log.d("main", String.valueOf(item.getTitle()));
            Intent intent = new Intent(this, NoteListActivity.class);
            intent.putExtra("groupName",item.getTitle());
            startActivity(intent);
            return false;
        });
        // PopupMenu关闭事件
/*        popupMenu.setOnDismissListener(menu ->
                Toast.makeText(getApplicationContext(), "关闭PopupMenu", Toast.LENGTH_SHORT).show()
        );*/
        popupMenu.show();
    }

    @Override
    protected void onResume() {
        super.onResume();
        SharedPreferences data = getSharedPreferences("data", MODE_PRIVATE);
        if(data.getString("userName","").isEmpty()){
            Intent intent=new Intent(SettingActivity.this,LoginActivity.class);
            startActivity(intent);
        }
    }
}